package service

import (
	"mime/multipart"
	conf "my-systems/pet-system/configs"
	"my-systems/pet-system/internal/info"
	"my-systems/pet-system/internal/models"
	"my-systems/pet-system/internal/sql/pet_sql"
	"my-systems/pet-system/pkg/e"
	"my-systems/pet-system/pkg/tools"
	"net/http"
)

type Pet struct {
	ID           uint    `json:"id" form:"id"`
	OwnerID      int64   `json:"owner_id" form:"owner_id"`           // 宠物主人的用户 ID -- 没有主人ID的宠物可以被领养
	Name         string  `json:"name" form:"name"`                   // 宠物名称
	Type         string  `json:"type" form:"type"`                   // 宠物种类，如猫、狗、鸟等
	Breed        string  `json:"breed" form:"breed"`                 // 宠物品种，如布偶猫、哈士奇等
	Age          int     `json:"age" form:"age"`                     // 宠物年龄
	Gender       string  `json:"gender" form:"gender"`               // 宠物性别，如公、母
	Color        string  `json:"color" form:"color"`                 // 宠物毛色
	Weight       float64 `json:"weight" form:"weight"`               // 宠物体重
	HealthStatus string  `json:"health_status" form:"health_status"` // 宠物健康状态
	BirthDate    string  `json:"birth_date" form:"birth_date"`       // 宠物出生日期
	AdoptionDate string  `json:"adoption_date" form:"adoption_date"` // 宠物领养日期
	VaccineInfo  string  `json:"vaccine_info" form:"vaccine_info"`   // 宠物接收疫苗情况
	PetAvatar    string  `json:"pet_avatar" form:"pet_avatar"`       // 宠物封面
}

type PetsAreAvailableForSale struct {
	ID           uint    `json:"id" form:"id"`
	Type         string  `json:"type" form:"type"`                   // 宠物种类，如猫、狗、鸟等
	Breed        string  `json:"breed" form:"breed"`                 // 宠物品种，如布偶猫、哈士奇等
	Age          int     `json:"age" form:"age"`                     // 宠物年龄
	Gender       string  `json:"gender" form:"gender"`               // 宠物性别，如公、母
	Color        string  `json:"color" form:"color"`                 // 宠物毛色
	Weight       float64 `json:"weight" form:"weight"`               // 宠物体重
	HealthStatus string  `json:"health_status" form:"health_status"` // 宠物健康状态
	BirthDate    string  `json:"birth_date" form:"birth_date"`       // 宠物出生日期
	VaccineInfo  string  `json:"vaccine_info" form:"vaccine_info"`   // 宠物接收疫苗情况
	PetAvatar    string  `json:"pet_avatar" form:"pet_avatar"`       // 宠物封面
	Price        string  `json:"price" form:"price"`                 // 宠物价格
	Description  string  `json:"description" form:"description"`     // 宠物详细描述
	IsAvailable  bool    `json:"is_available" form:"is_available"`   // 宠物是否在售
	Location     string  `json:"location" form:"location"`           // 宠物所在地理位置
	ContactInfo  string  `json:"contact_info" form:"contact_info"`   // 卖家联系方式

}

func (s *PetsAreAvailableForSale) GetSalePets(available bool) info.Response {
	code := http.StatusOK
	pets, err := pet_sql.GetSalePets(available)
	if err != nil {
		code = http.StatusBadRequest
		return info.Response{
			Status: code,
			Msg:    "查询在售宠物信息异常",
			Error:  err.Error(),
		}
	}
	return info.Response{
		Status: code,
		Data:   pets,
	}
}

func (s *PetsAreAvailableForSale) GetSalePetByID(pid string) interface{} {
	code := http.StatusOK
	var (
		petInfo *models.PetsAreAvailableForSale
		err     error
	)

	petInfo, err = pet_sql.GetSalePetByID(pid)
	if err != nil {
		code = http.StatusBadRequest
		return info.Response{
			Status: code,
			Msg:    "查询在售宠物信息异常",
			Error:  err.Error(),
		}
	}

	return info.Response{
		Status: code,
		Data:   petInfo,
	}
}

// 获取用户所有的宠物
func (p *Pet) GetALlUserPet(uid int) info.Response {
	code := http.StatusOK
	pets, err := pet_sql.GetALlUserPet(uid)
	if err != nil {
		code = http.StatusBadRequest
		return info.Response{
			Status: code,
			Msg:    "查询用户宠物信息异常",
			Error:  err.Error(),
		}
	}
	if len(pets) == 0 {
		return info.Response{
			Status: code,
			Data:   nil,
			Msg:    "抱歉亲亲，你名下还未有宠物哦",
			Error:  "",
		}
	} else {
		return info.Response{
			Status: code,
			Data:   pets,
			Msg:    "查询用户宠物信息成功",
		}
	}
}

// AddPets 添加宠物
func (p *Pet) AddPets(uid int, file multipart.File) info.Response {
	code := http.StatusOK
	var (
		avatarPath string
		err        error
	)

	if file != nil {
		avatarPath, err = UploadAvatarToLocalStatic(uid, p.Name, file, "pet_avatar")
		if err != nil {
			return info.Response{
				Status: code,
				Msg:    "上传头像失败",
				Error:  err.Error(),
			}
		}
	} else if avatarPath == "" {
		avatarPath = "default_Pet_Image.png"
	}

	pet := &models.Pet{
		// UserID: 宠物主人的用户 ID
		UserID: int64(uid),
		// Name: 宠物名称
		Name: p.Name,
		// Type: 宠物种类，如猫、狗、鸟等
		Type: p.Type,
		// Breed: 宠物品种，如布偶猫、哈士奇等
		Breed: p.Breed,
		// Age: 宠物年龄
		Age: p.Age,
		// Gender: 宠物性别，如公、母
		Gender: p.Gender,
		// Color: 宠物毛色
		Color: p.Color,
		// Weight: 宠物体重
		Weight: p.Weight,
		// HealthStatus: 宠物健康状态
		HealthStatus: p.HealthStatus,
		// BirthDate: 宠物出生日期
		BirthDate: p.BirthDate,
		// AdoptionDate: 宠物领养日期
		AdoptionDate: p.AdoptionDate,
		// VaccineInfo: 宠物接收疫苗情况
		VaccineInfo: p.VaccineInfo,
		// PetAvatar: 宠物封面
		PetAvatar: avatarPath,
	}
	err = pet_sql.AddPets(pet)
	if err != nil {
		tools.Logger.Error(err)
		code = e.PetAddFailed
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
			Error:  err.Error(),
		}
	}

	// 存储图像
	pet.PetAvatar = conf.URLHost + conf.ServerPort + conf.AvatarPath + avatarPath
	// 存储图像
	err = pet_sql.AddPetPhoto(pet, pet.PetAvatar)
	if err != nil {
		return info.Response{
			Status: http.StatusBadRequest,
			Msg:    "存储宠物图像失败",
			Error:  err.Error(),
		}
	}

	return info.Response{
		Status: code,
		Msg:    e.GetMsgInfo(code) + "宠物添加成功",
	}
}

func (p *Pet) UpPetAvatar(uid int, file multipart.File) interface{} {
	code := http.StatusOK
	pet, _ := pet_sql.GetByIDPet(p.ID, uid)
	// 上传文件
	path, err := UploadAvatarToLocalStatic(int(p.ID), pet.Name, file, "pet_avatar")
	if err != nil {
		return info.Response{
			Status: http.StatusBadRequest,
			Msg:    "上传头像失败",
			Error:  err.Error(),
		}
	}
	pet.PetAvatar = path
	// 更新封面
	err = pet_sql.UpPetAvatar(int(p.ID), uid, pet)
	if err != nil {
		return info.Response{
			Status: http.StatusBadRequest,
			Msg:    "上传宠物图像失败",
			Error:  err.Error(),
		}
	}
	pet.PetAvatar = conf.URLHost + conf.ServerPort + conf.AvatarPath + path
	// 存储图像
	err = pet_sql.AddPetPhoto(pet, pet.PetAvatar)
	if err != nil {
		return info.Response{
			Status: http.StatusBadRequest,
			Msg:    "存储宠物图像失败",
			Error:  err.Error(),
		}
	}
	return info.Response{
		Status: code,
		Msg:    "更新成功" + e.GetMsgInfo(code),
		Data:   "宠物封面地址：" + pet.PetAvatar,
	}
}

func (p *Pet) UpdatePet(uid int, pid uint, tmp *Pet) info.Response {
	code := http.StatusOK
	// 检查宠物是否存在
	_, exist, err := pet_sql.ExistOrNotByID(pid, uid)
	if err != nil && exist == false {
		code = e.ThePetNotExists
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
			Error:  err.Error(),
		}
	}

	// 宠物存在则进行更新
	pet := &models.Pet{
		// Name: 宠物名称
		Name: p.Name,
		// Type: 宠物种类，如猫、狗、鸟等
		Type: p.Type,
		// Breed: 宠物品种，如布偶猫、哈士奇等
		Breed: p.Breed,
		// Age: 宠物年龄
		Age: p.Age,
		// Gender: 宠物性别，如公、母
		Gender: p.Gender,
		// Color: 宠物毛色
		Color: p.Color,
		// Weight: 宠物体重
		Weight: p.Weight,
		// HealthStatus: 宠物健康状态
		HealthStatus: p.HealthStatus,
		// BirthDate: 宠物出生日期
		BirthDate: p.BirthDate,
		// AdoptionDate: 宠物领养日期
		AdoptionDate: p.AdoptionDate,
		// VaccineInfo: 宠物接收疫苗情况
		VaccineInfo: p.VaccineInfo,
	}
	err = pet_sql.UpdatePet(nil, pid, uid, pet)
	if err != nil {
		code = http.StatusBadRequest
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
			Error:  err.Error(),
		}
	} else {
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code) + "宠物信息更新成功",
		}
	}

}

func (p *Pet) DeletePet(pid uint, uid int) info.Response {
	// 检查宠物是否存在
	code := http.StatusOK
	// 检查宠物是否存在
	_, exist, err := pet_sql.ExistOrNotByID(pid, uid)
	if err != nil && exist == false {
		code = e.ThePetNotExists
		return info.Response{
			Status: code,
			Msg:    e.GetMsgInfo(code),
			Error:  err.Error(),
		}
	}
	// 存在则删除
	err = pet_sql.DeletePet(nil, pid, uid)
	if err != nil {
		code = http.StatusBadRequest
		return info.Response{
			Status: code,
			Msg:    "注销宠物信息失败",
			Error:  err.Error(),
		}
	}
	// 删除宠物图像
	err = pet_sql.DeletePetPhoto(pid)
	if err != nil {
		code = http.StatusBadRequest
		return info.Response{
			Status: code,
			Msg:    "注销宠物图像列表失败",
			Error:  err.Error(),
		}
	}
	return info.Response{
		Status: code,
		Msg:    "注销宠物信息成功",
	}
}
